package com.yunc.base.security.filter;

import cn.hutool.json.JSONUtil;
import com.yunc.base.utils.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 统一结果处理
 *
 * @author taocongcong
 * @create 2019-10-12 11:34
 */
public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        // String reason = "统一处理，原因："+authException.getMessage();
        response.getWriter().write(JSONUtil.toJsonStr(R.error(HttpServletResponse.SC_FORBIDDEN, authException.getMessage())));
    }

}
